<template>
  <div class="route-panel">
    <div class="route-panel-header">
      <div>
        <h3>围栏配置</h3>
        <p class="route-panel-sub">创建和编辑地图围栏范围</p>
      </div>
      <button class="route-close" @click.stop="emit('close')">✖</button>
    </div>
    <div class="route-panel-body">
      <div class="route-form-row">
        <label>围栏名称</label>
        <input
          class="route-input"
          :value="fenceForm.name"
          placeholder="请输入围栏名称"
          @input="handleBaseInput('name', $event)"
        />
      </div>
      <div class="route-form-row">
        <label>围栏描述</label>
        <textarea
          class="route-input"
          rows="2"
          :value="fenceForm.description"
          placeholder="填写围栏说明"
          @input="handleBaseInput('description', $event)"
        ></textarea>
      </div>
      <div class="route-form-row">
        <label>围栏类型</label>
        <select
          class="route-input"
          :value="fenceForm.type"
          @change="handleTypeChange($event)"
        >
          <option value="polygon">多边形</option>
          <option value="circle">圆形</option>
        </select>
      </div>
      <div class="route-actions">
        <button
          class="route-action primary"
          :disabled="fenceDrawingActive"
          @click.stop="emit('start-drawing')"
        >
          {{ fenceDrawingActive ? "绘制中..." : "开始绘制" }}
        </button>
        <button class="route-action" @click.stop="emit('clear')">
          清空围栏
        </button>
      </div>
      <div v-if="fenceForm.type === 'polygon'" class="waypoint-section">
        <div class="waypoint-header">围栏点列表</div>
        <div v-if="!fenceForm.points.length" class="waypoint-empty">
          暂未生成围栏点，点击“开始绘制”后在地图上标注围栏范围。
        </div>
        <div
          v-for="(fp, index) in fenceForm.points"
          :id="`fence-point-${index}`"
          :key="fp.id"
          class="waypoint-item"
          :class="{ active: fenceEditingIndex === index }"
          @click.stop="emit('focus-point', index)"
        >
          <div class="waypoint-title">
            <span>围栏点 {{ index + 1 }}</span>
            <i
              class="iconfont icon-shanchu"
              style="cursor: pointer; color: #f53f3f"
              @click.stop="emit('remove-point', index)"
            ></i>
          </div>
          <div class="waypoint-grid">
            <label>名称</label>
            <input
              class="route-input"
              :value="fp.name"
              placeholder="围栏点名称"
              @input="handlePointChange(index, 'name', $event)"
            />
            <label>纬度</label>
            <input
              class="route-input"
              type="number"
              step="0.000001"
              :value="fp.lat"
              @change="handlePointChange(index, 'lat', $event)"
            />
            <label>经度</label>
            <input
              class="route-input"
              type="number"
              step="0.000001"
              :value="fp.lng"
              @change="handlePointChange(index, 'lng', $event)"
            />
          </div>
        </div>
      </div>
      <div v-else class="waypoint-section">
        <div class="waypoint-header">圆形围栏参数</div>
        <div class="waypoint-grid">
          <label>中心纬度</label>
          <input
            class="route-input"
            type="number"
            :value="fenceForm.centerLat"
            @change="handleBaseInput('centerLat', $event)"
          />
          <label>中心经度</label>
          <input
            class="route-input"
            type="number"
            :value="fenceForm.centerLng"
            @change="handleBaseInput('centerLng', $event)"
          />
          <label>半径 (米)</label>
          <input
            class="route-input"
            type="number"
            min="0"
            step="1"
            :value="fenceForm.radius"
            @change="handleBaseInput('radius', $event)"
          />
        </div>
      </div>
      <div class="route-form-row">
        <label>围栏高度 (米)</label>
        <input
          class="route-input"
          type="number"
          min="0"
          step="1"
          :value="fenceForm.altitude"
          @change="handleBaseInput('altitude', $event)"
        />
      </div>
      <div class="route-form-row">
        <label>是否生效</label>
        <div class="switch-row">
          <input
            type="checkbox"
            :checked="!!fenceForm.active"
            @change="handleBaseInput('active', $event)"
          />
          <span>{{ fenceForm.active ? "启用" : "停用" }}</span>
        </div>
      </div>
    </div>
    <div class="route-panel-footer">
      <div class="route-footer-actions">
        <button class="route-submit" @click.stop="emit('submit')">
          保存围栏
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { defineEmits, defineProps, toRefs } from "vue";

const props = defineProps({
  fenceForm: {
    type: Object,
    required: true,
  },
  fenceDrawingActive: {
    type: Boolean,
    default: false,
  },
  fenceEditingIndex: {
    type: Number,
    default: null,
  },
});

const emit = defineEmits([
  "close",
  "start-drawing",
  "clear",
  "focus-point",
  "remove-point",
  "update-point-field",
  "update-field",
  "change-type",
  "submit",
]);

const { fenceForm, fenceDrawingActive, fenceEditingIndex } = toRefs(props);

const extractValue = (evt) => {
  if (!evt || !evt.target) return "";
  if (evt.target.type === "checkbox") {
    return !!evt.target.checked;
  }
  return evt.target.value;
};

const handleBaseInput = (field, evt) => {
  emit("update-field", { field, value: extractValue(evt) });
};

const handlePointChange = (index, field, evt) => {
  emit("update-point-field", {
    index,
    field,
    value: extractValue(evt),
  });
};

const handleTypeChange = (evt) => {
  emit("change-type", extractValue(evt));
};
</script>

<style scoped>
.route-panel {
  position: absolute;
  top: 12px;
  right: 45px;
  width: 320px;
  max-height: calc(100% - 24px);
  display: flex;
  flex-direction: column;
  background: rgba(17, 24, 39, 0.92);
  border-radius: 18px;
  box-shadow: 0 24px 48px rgba(0, 0, 0, 0.45);
  padding: 20px 18px;
  z-index: 904;
  overflow: hidden;
  color: #f9fafb;
  backdrop-filter: blur(10px);
}

.route-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
}

.route-panel-header h3 {
  margin: 0;
  font-size: 18px;
  color: #f3f4f6;
}

.route-panel-sub {
  margin: 4px 0 0;
  font-size: 12px;
  color: #9ca3af;
}

.route-close {
  border: none;
  background: transparent;
  color: #9ca3af;
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
}

.route-close:hover {
  color: #e5e7eb;
}

.route-panel-body {
  flex: 1;
  overflow-y: auto;
  margin-top: 12px;
  padding-right: 4px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.route-form-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.route-form-row label {
  font-size: 13px;
  color: #d1d5db;
}

.route-input,
.route-panel select {
  width: 100%;
  border: 1px solid rgba(148, 163, 184, 0.4);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
  background: rgba(17, 24, 39, 0.18);
  color: #f3f4f6;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.route-panel select {
  appearance: none;
}

.route-input:focus,
.route-panel select:focus {
  outline: none;
  border-color: #2563eb;
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.15);
}

.route-actions {
  display: flex;
  gap: 8px;
}

.route-action {
  flex: 1;
  border: 1px solid rgba(148, 163, 184, 0.4);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 13px;
  color: #e5e7eb;
  background: rgba(31, 41, 55, 0.6);
  cursor: pointer;
  transition: all 0.2s ease;
}

.route-action.primary {
  background: #2563eb;
  border-color: #2563eb;
  color: #fff;
}

.route-action.primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.route-action:not(.primary):hover {
  border-color: #9ca3af;
}

.route-action:disabled,
.route-action[disabled] {
  opacity: 0.6;
  cursor: not-allowed;
}

.waypoint-section {
  border: 1px solid rgba(148, 163, 184, 0.25);
  border-radius: 10px;
  background: rgba(30, 41, 59, 0.65);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.waypoint-header {
  font-size: 13px;
  font-weight: 600;
  color: #f3f4f6;
}

.waypoint-empty {
  font-size: 12px;
  color: #9ca3af;
}

.waypoint-item {
  padding: 10px;
  border-radius: 8px;
  background: rgba(31, 41, 55, 0.6);
  border: 1px solid rgba(148, 163, 184, 0.25);
  display: flex;
  flex-direction: column;
  gap: 8px;
  transition: border-color 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
}

.waypoint-item.active {
  border-color: rgba(59, 130, 246, 0.75);
  background: rgba(30, 64, 175, 0.35);
  box-shadow: 0 0 0 1px rgba(59, 130, 246, 0.4);
}

.waypoint-title {
  font-size: 13px;
  font-weight: 600;
  color: #f3f4f6;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.waypoint-grid {
  display: grid;
  grid-template-columns: 60px 1fr;
  gap: 6px 10px;
  align-items: center;
}

.waypoint-grid label {
  font-size: 12px;
  color: #cbd5f5;
}

.switch-row {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #e5e7eb;
}

.switch-row input {
  width: 16px;
  height: 16px;
}

.route-panel-footer {
  margin-top: 12px;
}

.route-footer-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

.route-submit {
  border: none;
  background: linear-gradient(135deg, #2563eb, #1d4ed8);
  color: #fff;
  padding: 10px 18px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 13px;
  box-shadow: 0 10px 24px rgba(37, 99, 235, 0.35);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.route-submit:hover {
  transform: translateY(-1px);
  box-shadow: 0 12px 24px rgba(37, 99, 235, 0.3);
}

.route-panel-enter-active,
.route-panel-leave-active {
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.route-panel-enter-from,
.route-panel-leave-to {
  opacity: 0;
  transform: translateX(12px);
}

@media (max-width: 1080px) {
  .route-panel {
    width: calc(100% - 24px);
    top: auto;
    bottom: 12px;
    right: 12px;
    left: 12px;
    max-height: 360px;
  }
}
</style>
